package com.fable.ntzw_ga.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fable.common.constants.CommonConstant;
import com.fable.common.vo.ResultVO;
import com.fable.ntzw_ga.entity.FeastType;
import com.fable.ntzw_ga.service.IFeastTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * @author Created by gzh 2023-01-31 23:12:00
 */
@Api(tags = "酒席创建管理")
@RestController
@RequestMapping("/ntzw_ga/feast-type")
@PreAuthorize("hasRole('admin')")
public class FeastTypeController {

    @Autowired
    IFeastTypeService feastTypeService;

    @ApiOperation(value = "酒席列表")
    @PostMapping ("/list")
    public ResultVO getDataList() {
        LambdaQueryWrapper<FeastType> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(FeastType::getFeastTime).last(CommonConstant.LIMIT);
        return ResultVO.okResult(feastTypeService.list(wrapper));
    }

    @ApiOperation(value = "添加酒席")
    @PostMapping("save")
    public ResultVO saveFeastType(@RequestBody FeastType feastType) {
        LambdaQueryWrapper<FeastType> wrapper = new LambdaQueryWrapper<FeastType>().eq(FeastType::getFeastStatus, true);
        FeastType one = feastTypeService.getOne(wrapper);
        if (one != null){
            one.setFeastStatus(false);
            feastTypeService.saveOrUpdate(one);
        }
        feastType.setFeastStatus(true);
        feastType.setFeastTime(LocalDate.now());
        return ResultVO.okResult(feastTypeService.save(feastType));
    }

    @ApiOperation(value = "修改酒席")
    @PutMapping("update")
    public ResultVO updateFeastType(@RequestBody FeastType feastType) {
        return ResultVO.okResult(feastTypeService.update(feastType, new LambdaQueryWrapper<FeastType>().eq(FeastType::getId, feastType.getId())));
    }

    @ApiOperation(value = "获取单个酒席信息")
    @GetMapping("getOne/{id}")
    public ResultVO getOne(@PathVariable String id) {
        return ResultVO.okResult(feastTypeService.getById(id));
    }

    @ApiOperation(value = "删除酒席")
    @PostMapping("delete")
    public ResultVO deleteFeastType(@RequestBody List<String> ids) {
        feastTypeService.removeByIds(ids);
        return ResultVO.okResult();
    }
}
